<?php

/** 
 * This is the model class for table "{{special}}". 
 * 
 * The followings are the available columns in table '{{special}}': 
 * @property string $id
 * @property string $name
 * @property integer $add_time
 * @property string $remark
 */ 
class SpecialModel extends BaseModel
{

    const STATUS_NOT = 0;//不可用
    const STATUS_HAS = 1;//可用

    const SPECIAL_NOT = 0;//不属于专场
    const SPECIAL_HAS = 1;//是属于专场  
      
    /** 
     * @return string the associated database table name 
     */ 
    public function tableName() 
    { 
        return '{{special}}'; 
    } 

    /** 
     * @return array validation rules for model attributes. 
     */ 
    public function rules() 
    { 
        // NOTE: you should only define rules for those attributes that 
        // will receive user inputs. 
        return array( 
            array('name', 'required'),
            array('add_time,listorder', 'numerical', 'integerOnly'=>true),
            array('name', 'length', 'max'=>50),
            array('status,remark,img,url,listorder,is_special,bm_img', 'safe'),
            // The following rule is used by search(). 
            // @todo Please remove those attributes that should not be searched. 
            array('id, name, add_time, remark,url,listorder', 'safe', 'on'=>'search'), 
        ); 
    } 

    /** 
     * @return array relational rules. 
     */ 
    public function relations() 
    { 
        // NOTE: you may need to adjust the relation name and the related 
        // class name for the relations automatically generated below. 
        return array( 
        ); 
    } 

    /** 
     * @return array customized attribute labels (name=>label) 
     */ 
    public function attributeLabels() 
    { 
        return array( 
            'id' => 'ID',
            'name' => '专场名称',
            'add_time' => '添加时间',
            'remark' => '备注',
            'status' => '状态',
            'img' => '图片路径',
            'url' => 'url地址',
            'listorder' => '排序',
            'bm_img' => '报名展示图片',
            'is_special' => '是否是属于专场',
        ); 
    } 

    /** 
     * Retrieves a list of models based on the current search/filter conditions. 
     * 
     * Typical usecase: 
     * - Initialize the model fields with values from filter form. 
     * - Execute this method to get CActiveDataProvider instance which will filter 
     * models according to data in model fields. 
     * - Pass data provider to CGridView, CListView or any similar widget. 
     * 
     * @return CActiveDataProvider the data provider that can return the models 
     * based on the search/filter conditions. 
     */ 
    public function search() 
    { 
        // @todo Please modify the following code to remove attributes that should not be searched. 

        $criteria=new CDbCriteria; 

        $criteria->compare('id',$this->id,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('add_time',$this->add_time);
        $criteria->compare('remark',$this->remark,true);

        return new CActiveDataProvider($this, array( 
            'criteria'=>$criteria, 
        )); 
    } 

    /** 
     * Returns the static model of the specified AR class. 
     * Please note that you should have this exact method in all your CActiveRecord descendants! 
     * @param string $className active record class name. 
     * @return SpecialModul the static model class 
     */ 
    public static function model($className=__CLASS__) 
    { 
        return parent::model($className); 
    }

    public function beforeSave()
    {
        $this->remark = CHtml::encode($this->remark);
        $this->remark = nl2br($this->remark);
        if ($this->isNewRecord)
        {
            $this->add_time = time();
        }
        return true;
    }

    public static function setIsSpecialHmtlList()
    {
        return array(
            self::SPECIAL_NOT => '否',
            self::SPECIAL_HAS => '是',
        );
    }

    public static function setIsSpecialHmtlDetail($data)
    {
        $arrData = self::setIsSpecialHmtlList();
        return $arrData[$data];
    }

    public function createSearchCriteria()
    {
        $criteria = new CDbCriteria; 

        $criteria->compare('name',$this->name,true);
        $criteria->compare('remark',$this->remark,true);

        return $criteria;      
    }

    public static function getStatusHtmlList()
    {
        return array(
            self::STATUS_NOT => '不显示',
            self::STATUS_HAS => '显示',
        );
    }

    public static function getStatusHtmlDetail($data)
    {
        $arrData = self::getStatusHtmlList();
        return  $arrData[$data];
    }

    public function saveImg(){
        $arrResult = array();
        if (UploadFile::getInstance()->upload()) {
            $arrData = UploadFile::getInstance()->getUploadFileInfo();
            foreach($arrData as $key => $val){
                $path = ConfigModel::model()->getConfig('sitefileurl') . $val['savepath'] . $val['savename'];
                $arrResult[$val['key']] = $path;
            }
        }
        return $arrResult;        
    }
} 